package tech.summerly.weather.component.adapter.floors

import android.view.LayoutInflater
import android.view.ViewGroup
import kotlinx.android.synthetic.main.floor_hourly_forecast.view.*
import tech.summerly.weather.R
import tech.summerly.weather.component.adapter.HourlyTrendAdapter
import tech.summerly.weather.component.adapter.decoration.TemperatureItemDecoration
import tech.summerly.weather.data.bean.HourlyForecast
import tech.summerly.weather.util.ViewHolder

class HourlyForecastFloor : BaseFloor<HourlyForecast>() {
    companion object {
        private const val I_DECOR_TEMPERATURE = 0
    }

    override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup): ViewHolder {
        val inflated = inflater.inflate(R.layout.floor_hourly_forecast, parent, false)
        inflated.recyclerHourly.addItemDecoration(TemperatureItemDecoration(), I_DECOR_TEMPERATURE)
        inflated.recyclerHourly.adapter = HourlyTrendAdapter()
        return ViewHolder(inflated)
    }

    override fun onBindViewHolder(holder: ViewHolder, item: HourlyForecast) = with(holder.itemView) {

        //        val colorPrimary = color(R.color.color_primary_dark)
//        chartHourly.reset()
//        chartHourly.setLabelsColor(colorPrimary)
//        val temperature = item.temperature.take(24)
//        val dateTime = temperature.map { it.datetime }.toTypedArray()
//        dateTime.forEachIndexed { index, value ->
//            if (index % 3 != 0) {
//                dateTime[index] = ""
//            } else {
//                dateTime[index] = value.substringAfterLast(" ")
//            }
//        }
//        val dataset = LineSet(dateTime, temperature.map { it.value }.toFloatArray())
//        dataset.setColor(colorPrimary)
//                .setDotsColor(colorPrimary)
//                .setThickness(4f)
//                .setSmooth(SettingsActivity.isChartLineSmooth())
//
//        chartHourly.addData(dataset)
//        chartHourly.setYLabels(AxisRenderer.LabelPosition.OUTSIDE)
//                .setLabelsFormat(DecimalFormat("#"))
//                .setXAxis(false)
//                .show()
        (recyclerHourly.adapter as? HourlyTrendAdapter)?.setData(item)

        (recyclerHourly.getItemDecorationAt(I_DECOR_TEMPERATURE) as? TemperatureItemDecoration)?.let {
            it.setData(item.temperature.map { it.value })
            recyclerHourly.invalidateItemDecorations()
        }
        textDescription.text = item.description
        Unit
    }
}